﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Nightlife.Models;

namespace Nightlife.Controllers.API
{
    public class SpotController : ApiController
    {
        private NightlifeDBContext db = new NightlifeDBContext();

        // GET api/Spot
        public IEnumerable<SpotModel> GetSpotModels()
        {
            return db.Spots.AsEnumerable();
        }

        // GET api/Spot/5
        public SpotModel GetSpotModel(int id)
        {
            SpotModel spotmodel = db.Spots.Find(id);
            if (spotmodel == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return spotmodel;
        }

        // PUT api/Spot/5
        public HttpResponseMessage PutSpotModel(int id, SpotModel spotmodel)
        {
            if (ModelState.IsValid && id == spotmodel.ID)
            {
                db.Entry(spotmodel).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK, spotmodel);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Spot
        public HttpResponseMessage PostSpotModel(SpotModel spotmodel)
        {
            if (ModelState.IsValid)
            {
                db.Spots.Add(spotmodel);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, spotmodel);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = spotmodel.ID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Spot/5
        public HttpResponseMessage DeleteSpotModel(int id)
        {
            SpotModel spotmodel = db.Spots.Find(id);
            if (spotmodel == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.Spots.Remove(spotmodel);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, spotmodel);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}